<template>
  <div class="button-bar">
    <div class="check-content">
      <cart-check-button class="check-button" :is-checked="isSelectAll" @click.native="buttonChange" />
      <span>全选</span>
    </div>
    <div class="price">
      已选 {{ checkLength }} 件
      合计：{{ totalPrice }}
    </div>
    <div class="calculate" @click="calcClick">结算</div>
  </div>
</template>

<script>
import CartCheckButton from "./CartCheckButton"
import { mapGetters } from 'vuex'
export default {
  name: "CartButtonBar",
  components: {
    CartCheckButton,
  },
  computed: {
    ...mapGetters(['cartList']),
    totalPrice() {
      return '￥' + this.cartList.filter(item => {
        return item.checked
      }).reduce((oldValue, item) => {
        let num = parseFloat(item.price.substr(1, item.price.length - 1))
        return oldValue + num * item.count
      }, 0)
    },
    checkLength() {
      return this.cartList.filter(item => item.checked).length
    },
    isSelectAll() {
      // 购物车为空时
      if (this.cartList.length === 0) return false

      return !this.cartList.find(item => !item.checked)
    }
  },
  methods: {
    buttonChange() {
      if (this.isSelectAll) { // 全部选中
        this.cartList.forEach(element => element.checked = false);
      } else { // 没有全部选中(部分不选中)
        this.cartList.forEach(element => element.checked = true);
      }
      // this.cartList.forEach(element => element.checked = !this.isSelectAll);
    },
    calcClick() {
      // 全部没有选中 弹出吐司
      if (!this.isSelectAll) {
        this.$toast.show("您还没有选择宝贝哦~");
      } else {
        this.$toast.show()
      }
    }
  }
}
</script>

<style scoped>
.button-bar {
  font-weight: 700;
  display: flex;
  position: relative;
  top: -35px;
  height: 65px;
  line-height: 65px;
  font-size: 14px;
  background-color: #FFF;
  border-top: 2PX solid #EEE;
}

.check-content {
  display: flex;
  align-items: center;
  margin-left: 15px;
  margin-right: 5px;
  width: 90px;
}

.check-button {

  width: 20px;
  height: 20px;
  line-height: 18px;
}

.price {
  margin-left: -35px;
  flex: 1;
  overflow: hidden;
}

.button-bar .calculate {
  /* float: right!important; 在flex中不起作用*/
  /* margin-left: 50px; */
  text-align: center;
  width: 120px;
  background-color: #FF7F00;
  border-radius: 45px;
  color: #fff;

}
</style>
